#include once "win\Tlhelp32.bi"

function ThreadsGet(TLIST() as integer) as integer  
  dim as ThreadEntry32 TINFO
  dim as integer CURPROC = GetCurrentProcessID()  
  dim as handle SNAPHAN = CreateToolhelp32Snapshot(TH32CS_SNAPALL,CURPROC)
  dim as integer COUNT
  redim TLIST(15)  
  with TINFO
  .dwSize = sizeof(ThreadEntry32)
  if Thread32First(SNAPHAN,@TINFO) then
    do
      if .th32OwnerProcessID = CURPROC then
        if COUNT > ubound(TLIST) then
          redim preserve TLIST(COUNT+15)          
        end if
        TLIST(COUNT) = .th32ThreadID
        COUNT += 1
      end if  
    loop while Thread32Next(SNAPHAN,@TINFO)
  else    
  end if
  end with
  redim preserve TLIST(COUNT-1)
  CloseHandle(SNAPHAN)
  return COUNT  
end function

function ThreadsGfxUse(TGFX() as handle,TLIST0() as integer,TLIST1() as integer) as integer
  dim as integer CNT,CNT2,TQTD    
  for CNT = 0 to ubound(TLIST1)  
    for CNT2 = 0 to ubound(TLIST0)
      if TLIST1(CNT) = TLIST0(CNT2) then exit for
    next CNT2
    if CNT2 > ubound(TLIST0) then
      TQTD += 1
      if TQTD > ubound(TGFX) then 
        redim preserve TGFX(TQTD+3)
      end if      
      TGFX(TQTD) = OpenThread(THREAD_ALL_ACCESS,null,TLIST1(CNT))      
      SetThreadPriority(TGFX(TQTD),Thread_Priority_Time_Critical)
    end if    
  next CNT
  redim preserve TGFX(TQTD)
  return TQTD
end function

function ThreadsGfxPause(TGFX() as handle) as integer
  if ubound(TGFX) < 1 then return 0
  dim as integer RESU
  for CNT as integer = 1 to ubound(TGFX)
    if SuspendThread(TGFX(CNT)) <> -1 then RESU += 1    
  next CNT
  return RESU
end function

function ThreadsGfxResume(TGFX() as handle) as integer
  if ubound(TGFX) < 1 then return 0
  dim as integer RESU
  for CNT as integer = 1 to ubound(TGFX)
    if ResumeThread(TGFX(CNT)) <> -1 then RESU += 1    
  next CNT
  return RESU
end function

function ThreadsGfxRelease(TGFX() as handle) as integer
  if ubound(TGFX) < 1 then return 0
  dim as integer RESU
  for CNT as integer = 1 to ubound(TGFX)
    CloseHandle(TGFX(CNT))
  next CNT
  return RESU
end function


  
